(function($) {

	var events_widget = {
    };

	$.fn.events_widget = function() {

		var options = {
			src_element: $(this),			
			eventsdata: []
		}

	    $.getJSON('resources/dummy/Events', function(data) {
	        options.eventsdata = data;
	        display_events(options);
	      });
	}

	function display_events(options) {
		var data = options.eventsdata;

		var events_container = $("<div>").addClass("dd_events_list");
		events_container.append($("<h3>").text("Event-Liste"));

		jQuery.each(data, function(key, eventitem) {
			var event_container = $("<div>").addClass("dd_eventlist_item span12 box no-left_margin");
			events_container.append(event_container);

			var temp_row = $("<div>").addClass("row-fluid");
			temp_row.append($("<div>").addClass("dd_name span12")
				.append($("<a href='event.shtml?id=id'>" + eventitem.name + "</a>")));
			event_container.append(temp_row);

			var temp_row = $("<div>").addClass("row-fluid");
			temp_row.append($("<div>").addClass("dd_description span12").text(eventitem.description));
			event_container.append(temp_row);

			var temp_row = $("<div>").addClass("row-fluid");
			temp_row.append($("<div>").addClass("dd_datetime span6").text(eventitem.time));
			temp_row.append($("<div>").addClass("dd_category span6").text(eventitem.category.name));
/* TODO
	            <div class='dd_category span6'>
	            	{{category.name}}
	            	<span class='dd_tooltip'>D
	                	<span>
	                  	{{category.description}}
	                	</span>
	              	</span>
	            </div>*/
			event_container.append(temp_row);

			var temp_row = $("<div>").addClass("row-fluid");
			temp_row.append($("<div>").addClass("dd_location span12")
				.text(eventitem.location.name + ", " + eventitem.location.zip + " " + eventitem.location.city));
			event_container.append(temp_row);

			var temp_row = $("<div>").addClass("row-fluid");
			var actions_container = $("<div>").addClass("d_actionlist_container dd_opener_container span6 box")
					.click('$(".dd_opener_content", this).toggle();');
			actions_container.append($("<h5>").text("Aufgaben"));
			jQuery.each(eventitem.actions.action, function(key1, action) {
				var action_row = $("<div>").addClass("dd_actionlist dd_opener_content");
				action_row.append($("<div>").addClass("dd_actionlist_item")
					.text(action.name + "<br />" + action.description + " -> <br />"));
				actions_container.append(action_row);		
				//ToDo: click verdrahten		
			});
			temp_row.append(actions_container);
			var contact_container = $("<div>").addClass("dd_contact_container dd_opener_container span6 box")
					.click('$(".dd_opener_content", this).toggle();');
			contact_container.append($("<h5>").text("Kontakt"));
			contact_container.append($("<div>").addClass("dd_contact dd_opener_content").text(eventitem.contact.fullname));
			//ToDo: click verdrahten
			temp_row.append(contact_container);
			event_container.append(temp_row);


		});
		$(options.src_element).append(events_container);

  	}

})(jQuery);